//
//  VCLogon.m
//  express
//
//  Created by mac1 on 15/9/21.
//  Copyright (c) 2015年 longchen. All rights reserved.
//

#import "VCLogon.h"

#import "VCRegister.h"

#import "DataCenter.h"

#import "Masonry.h"


#import "ZTTabBarController.h"

@interface VCLogon ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel* lbShanDiXia;

@property (nonatomic, strong) UIImageView* ivLogo;

@property (nonatomic, strong) UIView * viewContainer;

@property (nonatomic, strong) UITextField * tfPassword ;

@property (nonatomic, strong) UITextField * tfAccount;

@end

@implementation VCLogon

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:246/255.0 green:240/255.0 blue:217/255.0 alpha:1];

    [self.navigationController setNavigationBarHidden:YES animated:YES];

    [self addCtrls];
    
    // Do any additional setup after loading the view.
}


-(void)addCtrls
{
    //LOGO
    self.ivLogo = [[UIImageView alloc]initWithImage:nil];

    //self.ivLogo.backgroundColor = [UIColor blueColor];
    
    self.ivLogo.image = [UIImage imageNamed:@"Speedman"];

    [self.view addSubview:self.ivLogo];

    [self.ivLogo mas_makeConstraints:^(MASConstraintMaker *make)
     {

         make.centerX.mas_equalTo(self.view.mas_centerX);
         
         make.top.mas_equalTo(self.view.mas_top).offset(self.view.height/5);

     }];


//    //标题"闪电侠"
//    self.lbShanDiXia = [[UILabel alloc]init];
//
//    self.lbShanDiXia.text = @"快递侠";
//
//    self.lbShanDiXia.textAlignment = NSTextAlignmentCenter;
//
//    self.lbShanDiXia.textColor = [UIColor whiteColor];
//
//    [self.view addSubview:self.lbShanDiXia];
//
//    [self.lbShanDiXia mas_makeConstraints:^(MASConstraintMaker *make)
//     {
//         
//         make.centerX.mas_equalTo(self.view.mas_centerX);
//         
//         make.top.mas_equalTo(self.ivLogo.mas_bottom).offset(10);
//         
//         make.width.mas_equalTo(100);
//         
//         make.height.mas_equalTo(40);
//         
//     }];
    
    
#pragma mark 输入按钮 控件容器
    //输入 按钮 控件容器
    self.viewContainer  = [[UIView alloc]init];
    
    [self.view addSubview:self.viewContainer];
    
    [self.viewContainer mas_makeConstraints:^(MASConstraintMaker *make)
     {

        make.centerX.mas_equalTo(self.view.mas_centerX);

        make.top.mas_equalTo(self.view.mas_top).offset(self.view.height/2);

         make.width.mas_equalTo(self.view.mas_width);

         make.height.mas_equalTo(self.view.height/2);

     }];

    
    
    //账号输入框
    self.tfAccount = [[UITextField alloc]init];
    
    [self.viewContainer addSubview:self.tfAccount ];
    
    self.tfAccount.delegate = self;
    
    self.tfAccount .borderStyle = UITextBorderStyleNone;
    
    //左侧图标
    UIView * vAccountLeft = [[UIView alloc]init];
    
    self.tfAccount.leftView = vAccountLeft;
    
    self.tfAccount.leftViewMode = UITextFieldViewModeAlways;
    
    vAccountLeft.frame = CGRectMake(0, 0, 30, 30);
    
    UIImageView * ivAccountLeft = [[UIImageView alloc]init];
    
    [vAccountLeft addSubview:ivAccountLeft];
    
    ivAccountLeft.image = [UIImage imageNamed:@"icon_Phone"];
    
    ivAccountLeft.frame = CGRectMake(vAccountLeft.width/2-7, vAccountLeft.height/2-8, 14, 16);
    

    //背景
    UIImage * image = [UIImage imageNamed:@"Login_Input_Normal"];
    
    image = [image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:image.size.height/2];
    
    self.tfAccount.background = image;
    
    self.tfAccount .placeholder = @"您的手机号";
    
    self.tfAccount .textAlignment = NSTextAlignmentLeft;
    
    self.tfAccount.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    [self.tfAccount  mas_makeConstraints:^(MASConstraintMaker *make)
     {
         
         make.left.mas_equalTo(self.viewContainer.mas_left).offset(26);
         
         make.right.mas_equalTo(self.viewContainer.mas_right).offset(-26);
         
         make.top.mas_equalTo(10);
         
         make.height.mas_equalTo(30);
         
     }];
    
    
    //密码输入框
    self.tfPassword = [[UITextField alloc]init];
    
    [self.viewContainer addSubview:self.tfPassword ];
    
    self.tfPassword.delegate = self;
    
    self.tfPassword .borderStyle = UITextBorderStyleNone;
    
    //左侧图标
    UIView * vPasswordLeft = [[UIView alloc]init];
    
    self.tfPassword.leftView = vPasswordLeft;
    
    self.tfPassword.leftViewMode = UITextFieldViewModeAlways;
    
    vPasswordLeft.frame = CGRectMake(0, 0, 30, 30);
    
    UIImageView * ivPasswordLeft = [[UIImageView alloc]init];
    
    [vPasswordLeft addSubview:ivPasswordLeft];
    
    ivPasswordLeft.image = [UIImage imageNamed:@"icon_Pwd"];
    
    ivPasswordLeft.frame = CGRectMake(vPasswordLeft.width/2-7, vPasswordLeft.height/2-8,14, 16);
    
    
    //右侧按钮
    UIView * vPasswordRight = [[UIView alloc]init];
    
    self.tfPassword.rightView = vPasswordRight;
    
    self.tfPassword.rightViewMode = UITextFieldViewModeAlways;
    
    vPasswordRight.frame = CGRectMake(0, 2, 80, 26);

    UIButton * btnPasswordRight = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [vPasswordRight addSubview:btnPasswordRight];
    
    btnPasswordRight.frame = CGRectMake(0, 0, vPasswordRight.width-3, vPasswordRight.height);
    
    [btnPasswordRight setImage:[UIImage imageNamed:@"BtnVerify_Normal"] forState:UIControlStateNormal];
    
    [btnPasswordRight setImage:[UIImage imageNamed:@"BtnVerify_Pressed"] forState:UIControlStateHighlighted];
    
    [btnPasswordRight setTitle:@"获取验证码" forState:UIControlStateNormal];
    
    btnPasswordRight.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [btnPasswordRight setTitleEdgeInsets:UIEdgeInsetsMake(0, -84, 0, 0)];
    
    [btnPasswordRight addTarget:self action:@selector(getSMSCodeWithPhoneNumber:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //背景
    UIImage * imagePassword = [UIImage imageNamed:@"Login_Input_Normal"];
    
    imagePassword = [image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:image.size.height/2];
    
    self.tfPassword.background = image;
    
    self.tfPassword.placeholder = @"输入验证码";
    
    self.tfPassword.secureTextEntry = YES;
    
    self.tfPassword.textAlignment = NSTextAlignmentLeft;
    
    self.tfPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    [self.tfPassword  mas_makeConstraints:^(MASConstraintMaker *make)
     {
         
         make.left.mas_equalTo(self.viewContainer.mas_left).offset(26);
         
         make.right.mas_equalTo(self.viewContainer.mas_right).offset(-26);
         
         make.top.mas_equalTo(self.tfAccount.mas_bottom).offset(16);
         
         make.height.mas_equalTo(30);
         
     }];


    //登陆按钮
    UIButton * btnLogon = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    [self.viewContainer addSubview:btnLogon];
    
    btnLogon.backgroundColor = [UIColor blueColor];
    
    [btnLogon setTitle:@"登陆" forState:UIControlStateNormal];
    
    [btnLogon addTarget:self action:@selector(btnLogon:) forControlEvents:UIControlEventTouchUpInside];
    
    [btnLogon setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [btnLogon mas_makeConstraints:^(MASConstraintMaker *make)
     {
         
         make.centerX.mas_equalTo(self.viewContainer.mas_centerX);
         
         make.top.mas_equalTo(self.tfPassword.mas_bottom).offset(20);
         
         make.width.mas_equalTo(180);
         
         make.height.mas_equalTo(30);
         
     }];


    //忘记密码
    UIButton * btnForgetSecret = [UIButton buttonWithType:UIButtonTypeSystem];

    [self.viewContainer addSubview:btnForgetSecret];

    [btnForgetSecret setTitle:@"忘记密码" forState:UIControlStateNormal];

    [btnForgetSecret setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

    btnForgetSecret.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    [btnForgetSecret mas_makeConstraints:^(MASConstraintMaker *make)
     {

         make.left.mas_equalTo(btnLogon.mas_left);

         make.top.mas_equalTo(btnLogon.mas_bottom).offset(10);

         make.width.mas_equalTo(80);

         make.height.mas_equalTo(30);

     }];


    //注册
    UIButton * btnRegister = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [self.viewContainer addSubview:btnRegister];
    
    [btnRegister setTitle:@"登陆注册" forState:UIControlStateNormal];
    
    [btnRegister setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    btnRegister.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    [btnRegister mas_makeConstraints:^(MASConstraintMaker *make)
     {

         make.right.mas_equalTo(btnLogon.mas_right);

         make.top.mas_equalTo(btnLogon.mas_bottom).offset(10);

         make.width.mas_equalTo(80);

         make.height.mas_equalTo(30);

     }];

    [btnRegister addTarget:self action:@selector(btnClkRegister:) forControlEvents:UIControlEventTouchUpInside];

}


-(void)btnClkRegister:(UIButton*)btn
{

    VCRegister * vcRegister = [[VCRegister alloc]init];
    
    [self.navigationController pushViewController:vcRegister animated:YES];

}


- (void)didReceiveMemoryWarning
{

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
        //背景
    UIImage * image = [UIImage imageNamed:@"Login_Input_Normal"];
    
    image = [image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:image.size.height/2];
    
    textField.background = image;
}


-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //背景
    UIImage * image = [UIImage imageNamed:@"Login_Input_focus"];
    
    image = [image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:image.size.height/2];
    
    textField.background = image;
    
    [UIView animateWithDuration:0.5 animations:^{

        [self.viewContainer mas_updateConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self.view.mas_top).offset(SCREEN_HEIGHT/4);

            self.viewContainer.y = SCREEN_HEIGHT/4;
            
            self.ivLogo.alpha = 0.1;
            
            self.lbShanDiXia.alpha = 0.1;

        }];
        
    }];
    
    return YES;

}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];

    [UIView animateWithDuration:0.5 animations:^{

        [self.viewContainer mas_updateConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self.view.mas_top).offset(SCREEN_HEIGHT/2);

            self.viewContainer.y = SCREEN_HEIGHT/2;

            self.ivLogo.alpha = 1;

            self.lbShanDiXia.alpha = 1;

        }];
        
    }];

    return YES;
}


-(void)getSMSCodeWithPhoneNumber:(UIButton*)btnSMS
{
     [[DataCenter Create]getSMSCodeWithPhoneNumber:self.tfAccount.text];
}


#pragma - mark 登陆按钮
-(void)btnLogon:(UIButton*)logon
{

    ZTTabBarController * tbc = [[ZTTabBarController alloc]init];
    
    [self presentViewController:tbc animated:YES completion:nil];
//    
//    DataCenter * data = [DataCenter Create];
//    
//    [data loginWithAccount:self.tfAccount.text withPassword:self.tfPassword.text];
//    
//    //登陆成功
//    [data setLoginSuccess:^{
//       NSLog ( @" 登陆成功" );
//       
//       //创建UItabbar
//       ZTTabBarController * tbc = [[ZTTabBarController alloc]init];
//       
//       [self presentViewController:tbc animated:YES completion:nil];
//       
//   }];
//
//
//    //登陆失败
//    [data setLoginError:^(NSString *msg) {
//                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题"
//                                                                                 message:msg
//                                                                          preferredStyle:UIAlertControllerStyleAlert];
//
//        UIAlertAction*otherAction = [UIAlertAction actionWithTitle:@"OK"
//                                                             style:UIAlertActionStyleDefault
//                                                           handler:^(UIAlertAction*action) {
//                                                               NSLog(@"Okay");
//                                                               return ;
//                                                           }];
//
//        [alertController addAction:otherAction];
//
//        [self presentViewController:alertController animated:YES completion:nil];
//
//    }];
//
}


@end
